// pages/appeal/appeal.js
const App = getApp()
const teacherReduceType = {
  1: '未签到',
  2: '未点名',
  3: '未布置作业',
  4: '未课堂反馈',
  5: '未作业点评',
  6: '未拍照',
}

Page({
  data: {
    fileUrl: "",
    urlType: '',
    salaryInfo: {},
    teacherReduceType: teacherReduceType
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!options) return
    console.log(options)
    this.setData({
      id: options.id,
      type: options.type
    })
    this.getAppealDetail()
  },
  async getAppealDetail() {
    let data = await App.$axios.request({
      // 老师-扣费申诉详情
      url: "/teacher/curriculum/salaryComplaintInfo",
      method: 'GET',
      loading: true,
      data: {
        timetableDetailedId: this.data.id,
        curriculumType: this.data.type
      }
    })
    if (data) {
      this.setData({
        salaryInfo: data,
        fileUrl: data.complaintUrl ? data.complaintUrl : '',
        urlType: data.complaintUrl ? isImageOrVideo(data.complaintUrl) : ''
      })
    }
  },
  async uploadAppealFile() {
    // 上传申诉资料
    try {
      let files = await wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album']
      });
      let file = files.tempFilePaths[0]
      let suffix = file.substring(file.indexOf('.'), file.length)
      let url = await App.$axios.request({
        url: `/file/upload`,
        method: 'FILE',
        loading: true,
        filePath: file,
        name: 'file', // 文件字段名，默认为file
        formData: {
          name: `feedbackFile${suffix}`
        }
      })
      console.log(url)
      if (!url) return
      this.setData({
        fileUrl: url,
        urlType: isImageOrVideo(url)
      })
    } catch (error) {
      console.log(error)
    }
  },
  complaintResultChange(data) {
    // 文本域内容修改
    let content = data.currentTarget.dataset.type
    let value = data.detail.value
    this.setData({
      'salaryInfo.complaintResult': value
    })
  },
  async submitData() {
    let params = this.data.salaryInfo
    console.log(!params.complaintResult, !this.data.fileUrl)
    if (!params.complaintResult || !this.data.fileUrl) {
      wx.showToast({
        title: '请先完善申诉信息',
        icon: 'none'
      })
      return
    }
    let data = await App.$axios.request({
      // 老师-扣费申诉详情
      url: "/teacher/curriculum/salaryComplaint",
      method: 'PUT',
      loading: true,
      data: {
        timetableDetailId: this.data.id,
        curriculumType: this.data.type,
        complaintUrl: this.data.fileUrl,
        complaintResult: params.complaintResult
      }
    })
    if (data) {
      wx.showToast({
        title: '提交成功，请等待审批',
        icon: 'none'
      })
      this.getAppealDetail()
    }
  }
})

function isImageOrVideo(url) {
  // 获取文件扩展名
  const extension = url.split('.').pop();
  // 检查扩展名是否是图片或视频的常见类型
  const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif'];
  const videoExtensions = ['mp4', 'webm', 'ogg', 'mov', 'm4v', 'avi', 'wmv', 'flv'];
  return imageExtensions.includes(extension) ? 'image' : (videoExtensions.includes(extension) ? 'video' : 'unknown');
}